package com.sdtv.mediunfusion.mvp.ui.outsidetask;

import android.content.Context;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Autowired;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.blankj.utilcode.util.ToastUtils;
import com.sdtv.basemodule.annotations.CreatePresenter;
import com.sdtv.basemodule.annotations.PresenterVariable;
import com.sdtv.basemodule.base.BaseActivity;
import com.sdtv.basemodule.utils.ClickProxy;
import com.sdtv.mediunfusion.R;
import com.sdtv.mediunfusion.bean.DepartmentBean;
import com.sdtv.mediunfusion.bean.SelectTopicBean;
import com.sdtv.mediunfusion.mvp.contract.MainContract;
import com.sdtv.mediunfusion.mvp.presenter.SelectTopicListPresenter;
import com.sdtv.mediunfusion.utils.RouterPath;
import com.sdtv.mediunfusion.utils.Utils;
import com.sdtv.mediunfusion.widget.CustomeToolBar;
import com.sdtv.mediunfusion.widget.NewsCompontentSelectorDialogBuilder;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * 添加报题内容 或者是 显示报题的详细信息
 * <p>
 * --根据是否有 报题ID来区分
 */
@Route(path = RouterPath.TO_TOPIC_DETAIL_AND_ADD_ACTIVITY)
@CreatePresenter(presenter = {SelectTopicListPresenter.class,})
public class AddAndShowSelectTopicActivity extends BaseActivity implements
        NewsCompontentSelectorDialogBuilder.OnSaveLocationLister, MainContract.SelectTopicListView {

    @BindView(R.id.topic_titleEdit)
    EditText topicTitleEdit;
    @BindView(R.id.topic_desEdit)
    EditText topicDesEdit;
    @BindView(R.id.departmentName)
    TextView departmentName;
    @BindView(R.id.topic_remarksEdit)
    EditText topicRemarksEdit;
    @BindView(R.id.departmentImg)
    ImageView departmentImg;
    @BindView(R.id.addAndshow_rootView)
    RelativeLayout rootView;
    @BindView(R.id.as_cusBar)
    CustomeToolBar cusBar;
    // 获取部门ID
    private String departmentId;
    //获取数据的model  -
//    private SelectTopicModel dataModel;
    // 部门 组件
    private NewsCompontentSelectorDialogBuilder dialogBuilder;
    //分类列表 当前保存某一组件下所有的分类内容
    private List<DepartmentBean> claList = new ArrayList<>();
    //所有的组件名称
    private List<String> classifyNameList = new ArrayList<>();

    //判断是添加 还是 显示详情  true:是添加操作   false:显示详情
    private boolean isAdd;
    // 是否有选题ID 如果有 则说明是详情页
    @Autowired
    String selectTopicId;
    @PresenterVariable
    SelectTopicListPresenter presenter;

    /**
     * 设置布局文件
     *
     * @return
     */
    @Override
    public int getLayoutId() {
        return R.layout.activity_add_and_show_select_topic;
    }

    /**
     * 初始化页面布局
     */
    @Override
    public void initView() {

    }

    /**
     * 加载页面数据
     */
    @Override
    public void initData() {
        if (!TextUtils.isEmpty(selectTopicId)) {
            isAdd = false;
            //设置标题
            cusBar.setTitle("详情");
            //获取详情信息 并且设置editText不可点击  隐藏部门选择按钮 --- 展示详情
            departmentImg.setVisibility(View.GONE);
            // 获取详情 --
            presenter.getTopicInfo(selectTopicId);
            // 设置edittext不可编辑
            Utils.disableEditText(topicTitleEdit);
            Utils.disableEditText(topicDesEdit);
            Utils.disableEditText(topicRemarksEdit);
        } else {
            isAdd = true;
            cusBar.setTitle("报题");
            // 完成按钮---是添加报题
            cusBar.setRightText("完成");
            cusBar.showRightTab(true);
            //提交按钮点击事件
            cusBar.getTbLogText().setOnClickListener(clickProxy);
            //选择部门点击事件
            departmentName.setOnClickListener(clickProxy);
            // 获取部门列表内容---
//            dataModel.getDepartmentList();
        }
    }

    /**
     * 设置点击事件
     */
    private ClickProxy clickProxy = new ClickProxy(view -> {
        switch (view.getId()) {
            case R.id.tb_loggingText:
                //提交报题
//                addSelectTopicToServer();
                break;
            case R.id.departmentName:
                //强制隐藏软键盘
                InputMethodManager inputManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
                inputManager.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), 0); //强制隐藏键盘
                // 选择部门
                if (claList.isEmpty()) {
                    ToastUtils.showShort("获取部门列表异常");
                    return;
                }
                // 设置选中部门组件
                if (dialogBuilder == null) {
                    dialogBuilder = NewsCompontentSelectorDialogBuilder
                            .getInstance(this);
                    dialogBuilder.setOnSaveLocationLister(this);
                }
                dialogBuilder.setOnlyClassify("deparment");
                //默认选中第一项
                dialogBuilder.setClassifys(classifyNameList, 0);
                //获得当前窗体
                Window location_window = dialogBuilder.getWindow();
                //设置 dialog的显示位置
                WindowManager.LayoutParams locationLp = location_window.getAttributes();
                location_window.setGravity(Gravity.LEFT | Gravity.BOTTOM);
                //(当Window的Attributes改变时系统会调用此函数)
                location_window.setAttributes(locationLp);
                dialogBuilder.show();
                break;
            default:
        }
    });

    /**
     * 设置选中的部门名称-
     *
     * @param location
     * @param provinceId
     * @param cityId
     */
    @Override
    public void onSaveLocation(String location, String provinceId, String cityId) {
        // 设置选中部门
        departmentName.setText(location);
        //选中的部门ID
        departmentId = claList.get(Integer.parseInt(cityId)).getDepartmentId();

    }

    @Override
    public void setListData(int totalCount, List<SelectTopicBean> responseList) {

    }

    /**
     * 设置详情信息
     *
     * @param item
     */
    @Override
    public void setTopicInfo(SelectTopicBean item) {
        if (null != item) {
            // 设置信息---- 标题 ，描述   部门   备注
            topicTitleEdit.setText(item.getSelectTopicTitle());
            // 描述
            topicDesEdit.setText(item.getDescriptions());
            // 部门
            departmentName.setText(item.getDepartmentName());
            // 设置备注
            if (!TextUtils.isEmpty(item.getRemarks())) {
                topicRemarksEdit.setText(item.getRemarks());
            }
        }
    }
}
